package cn.edu.neusoft.zengweidong707.neufood.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.zengweidong707.neufood.R;
import cn.edu.neusoft.zengweidong707.neufood.activity.FoodListActivity;
import cn.edu.neusoft.zengweidong707.neufood.bean.ShopListBean;


/**
 * Created by Lucio on 2017/11/16.
 */

public class ShopListAdapter extends BaseAdapter<ShopListBean> {

    public ShopListAdapter(Context context, List<ShopListBean> items) {
        super(context, items);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context)
                .inflate(R.layout.items_shoplist, parent, false);
        ShopListViewHolder shopListViewHolder = new ShopListViewHolder(itemView);
        return shopListViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ShopListBean entity = items.get(position);
        ShopListViewHolder myViewHolder = (ShopListViewHolder)holder;
        //Picasso.with(context).load(items.get(position).getPic()).placeholder(R.drawable.ic_user)
                                                             //   .into((Target) myViewHolder.img_card);
        String photo = entity.getPic();
        if(!"".equals(photo))
        {
            Picasso.with(context)
                    .load(photo)
                    .into(myViewHolder.img_card);
        }
        else
        {
            myViewHolder.img_card.setImageResource(R.mipmap.ic_launcher);
        }
        myViewHolder.tv_shopname.setText(entity.getShopname());
        myViewHolder.tv_address.setText(entity.getAddress());
        myViewHolder.level.setRating(Float.parseFloat(entity.getLevel()));

        myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int shopid = entity.getShop_id();
                String shopnames = entity.getShopname();
                Intent intent = new Intent(context, FoodListActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("shopid",shopid);
                bundle.putString("shopname",shopnames);
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
    }

    public class ShopListViewHolder extends RecyclerView.ViewHolder {
        ImageView img_card;
        TextView tv_shopname;
        TextView tv_address;
        RatingBar level;
        ShopListViewHolder(View itemView) {
            super(itemView);
            img_card = (ImageView) itemView.findViewById(R.id.img_card);
            tv_shopname = (TextView) itemView.findViewById(R.id.tv_shopname);
            tv_address = (TextView) itemView.findViewById(R.id.tv_address);
            level = (RatingBar) itemView.findViewById(R.id.level);
        }
    }
}


